package ${domain}.frame.utils;

import ${domain}.frame.auth.LocalData;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * RequestUtil
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2017-01-01
 */
public class RequestUtil {

    /**
     * 获取请求放IP
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }

        } else if (ip.length() > 15) {
            String[] ips = ip.split(",");
            for (int index = 0; index < ips.length; index++) {
                String strIp = (String) ips[index];
                if (!("unknown".equalsIgnoreCase(strIp))) {
                    ip = strIp;
                    break;
                }
            }
        }
        return ip;
    }

    /**
     * 获取情况方客户端信息
     *
     * @param request 请求
     * @return 客户端信息
     */
    public static String getUserAgent(HttpServletRequest request) {
        return request.getHeader("User-Agent");
    }

    /**
     * 获取转发至错误页之前的请求URL
     *
     * @param request 请求
     * @return 请求URL
     */
    public static String getErrorUrl(HttpServletRequest request) {
        if (request.getAttribute("javax.servlet.error.request_uri") != null) {
            return (String) request.getAttribute("javax.servlet.error.request_uri");
        } else {
            return "";
        }
    }

    public static String getScheme(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        Pattern compile = Pattern.compile("(http|https)://(.*)/");
        Matcher matcher = compile.matcher(url);
        if (matcher.find()) {
            String group = matcher.group(1);
            return group;
        }
        return "";
    }

    public static String getDomain(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        Pattern compile = Pattern.compile("(http|https)://(.*)(:.*)?/");
        Matcher matcher = compile.matcher(url);
        if (matcher.find()) {
            String group = matcher.group(2);
            return group;
        }
        return "";
    }

    public static int getPort(HttpServletRequest request) {
        String url = request.getRequestURL().toString();
        Pattern compile = Pattern.compile("(http|https)://(.*)(:.*)?/");
        Matcher matcher = compile.matcher(url);
        if (matcher.find()) {
            String group = matcher.group(3);
            if (group == null) {
                return Integer.parseInt(group);
            }
        }
        return 80;
    }

    public static void setRedirect(String url) {
        HttpServletResponse response = LocalData.getResponse();
        try {
            response.sendRedirect(url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void setForward(String url) {
        HttpServletRequest request = LocalData.getRequest();
        HttpServletResponse response = LocalData.getResponse();
        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
    }

    public static String getHeader(HttpServletRequest request, String head, String defaultValue) {
        try {
            return request.getHeader(head);
        } catch (Exception e) {
            return defaultValue;
        }
    }

    public static String getHeader(HttpServletRequest request, String head) {
        return getHeader(request, head, null);
    }

    public static Pattern actionPattern = Pattern.compile("^/(.+)\\.htm");

    public static String getPage() {
        HttpServletRequest request = LocalData.getRequest();
        Matcher matcher = actionPattern.matcher(request.getServletPath());
        if (!matcher.find()) {
            return "";
        }
        return matcher.group(1);
    }
}
